#include "readFile.h"

#include "../global.h"
#include <bits/stdc++.h>
#include <unistd.h>
#include <vector>

using namespace std;

int getPath()
{
    char tempPath[255];
    size_t size=255;
    if(!getcwd(tempPath,size)){
        cout<<"Get path fialed!"<<endl;
        return -1;
    }
    cout<<"Current path:"<<tempPath<<endl;
    // strcpy(currentPath,tempPath);
    return 0;
}

char *readFileStr(char *fileName){
    
    char filePath[256];
    strcpy(filePath,currentPath);
    strcat(filePath,fileName);
    // cout<<"Reading  "<<filePath<<endl;
    ifstream fin(filePath);
    streambuf *oldcin;
    oldcin = cin.rdbuf(fin.rdbuf());
    string tempstr;
    string strRes;

    while(getline(fin,tempstr)){
        strRes+=tempstr;
        strRes+='\n';
    }
    // cout<<strRes<<endl;
    char *res=(char *)malloc((strRes.length()+1)*sizeof(char));
    strRes.copy(res,strRes.length(),0);
    // stringstream res(strRes);
    cin.rdbuf(oldcin);
    fin.close();
    // cout<<"Done reading "<<filePath<<endl<<endl;
    return res;
}

 vector<fileData> readFileData(char *fileName)
 {
    vector<fileData> fData;
    fileData tempFD;
    char *tempStr;
    tempStr=readFileStr(fileName);
    cout<<"Converting to data:"<<endl;
    stringstream tempStream(tempStr);
    while(tempStream>>tempFD.type){
        tempStream>>tempFD.value;
        fData.push_back(tempFD);
    }

    for(int k=0;k<fData.size();k++){
        cout<<"\t";
        cout<<fData[k].type<<"="<<fData[k].value<<endl;
    }
    
    cout<<endl;
    delete[] tempStr;
    return fData;
 }

int readFile(){
    return 0;
}

